package com.maomi.coder;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.analysis.core.StopAnalyzer;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
import org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;

import java.io.IOException;

/**
 * 描述:
 *
 * @author luzhongfu
 * @create 2019-03-28 11:43 PM
 */
public class AnalyzerDemo {

    private static String[] examples = new String[]{"The is big dog","ZY&Z Corportion-xyz@example.com"};

    private final static Analyzer[] analyzers = new Analyzer[]{
        new WhitespaceAnalyzer(),new SimpleAnalyzer(),new StopAnalyzer(),new StandardAnalyzer()
    };

    private static void anaylzer(String text) throws IOException {
        System.out.println("Analyzer:" + text);
        for (Analyzer analyzer:analyzers) {
            String name = analyzer.getClass().getSimpleName();
            System.out.println("analyzer name:"+name);

            displayToken(analyzer,text);
        }
    }

    private static void displayToken(Analyzer analyzer,String text) throws IOException {
        //获取tokenStream
        TokenStream tokenStream = analyzer.tokenStream("contents", text);

//        TermToBytesRefAttribute term = tokenStream.addAttribute(TermToBytesRefAttribute.class);

//        TypeAttribute typeAttribute = tokenStream.addAttribute(TypeAttribute.class);

        CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
        PositionIncrementAttribute positionIncrementAttribute = tokenStream.addAttribute(PositionIncrementAttribute.class);

        tokenStream.reset();
        while (tokenStream.incrementToken()){
//            System.out.println(term.getBytesRef());
//            System.out.println(typeAttribute.type());
            System.out.print(charTermAttribute);
//            System.out.println(positionIncrementAttribute.getPositionIncrement());
        }
        System.out.println();
        tokenStream.end();
        tokenStream.close();
    }

    public static void main(String[] args) throws IOException {
        for (String txt:examples) {
            anaylzer(txt);
        }
    }
}

